/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.BacDaoTaoM;
import model.DON_VI_TCM;
import model.NganhM;
import util.DAL;
import util.MyConnectUnit;

public class NganhD {
	private MyConnectUnit conn;

	/**
	 * @throws Exception
	 * @function constructor NganhD
	 */
	public NganhD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}

	/**
	 * @function get all Nganh
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NganhM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<NganhM> ls = new ArrayList<NganhM>();
		ResultSet rs = this.conn.Selects(
						"id_nganh, ma, n.ten, n.id_bac_dao_tao, bdt.ten, n.id_don_vi_ql, dvtc.ten",
						"NGANH n, BAC_DAO_TAO bdt, DON_VI_TC dvtc",
						"n.id_bac_dao_tao = bdt.id_bac_dao_tao AND n.id_don_vi_ql = dvtc.id_don_vi_tc AND n.ten LIKE '%" + ten + "%'",
						"ma ASC",
						limit);
		NganhM l = null;
		while (rs.next()) {
			l = new NganhM();
			l.setId_nganh(rs.getInt("id_nganh"));
			l.setMa(rs.getString("ma"));
			l.setTen(rs.getString("n.ten"));
			
			l.setId_bac_dao_tao(rs.getInt("n.id_bac_dao_tao"));
			BacDaoTaoM bdt = new BacDaoTaoM();
			bdt.setTen(rs.getString("bdt.ten"));
			l.setTen_bac_dao_tao(bdt);
			
			l.setId_don_vi_ql(rs.getInt("n.id_don_vi_ql"));
			DON_VI_TCM dvtc = new DON_VI_TCM();
			dvtc.setTen(rs.getString("dvtc.ten"));
			l.setTen_don_vi(dvtc);

			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all Nganh
	 * @param Condition
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NganhM> getAll(String Condition, String OrderBy) throws Exception {
		ArrayList<NganhM> ls = new ArrayList<NganhM>();
		ResultSet rs = this.conn.Select("NGANH", Condition, OrderBy);
		NganhM l = null;
		while (rs.next()) {
			l = new NganhM();
			l.setId_nganh(rs.getInt("id_nganh"));
			l.setMa(rs.getString("ma"));
			l.setTen(rs.getString("ten"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all Nganh with Condition
	 * @param Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NganhM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}

	/**
	 * @function get all Nganh no Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NganhM> getAll() throws Exception {
		return getAll(null);
	}

	/**
	 * @function search ten Nganh
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<NganhM> searchNganh(String ten) throws Exception {
		ArrayList<NganhM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}

	/**
	 * @function test Ma Nganh exits
	 * @param ma
	 * @return
	 * @throws Exception
	 */
	public int getMaNganh(String ma) throws Exception{
		ArrayList<NganhM> l = getAll("ma = '" + ma + "'");
		return l.size();
	}
	/**
	 * @function insert Nganh
	 * @param n
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(NganhM n) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ma", n.getMa());
		map.put("ten", n.getTen());
		map.put("id_bac_dao_tao", n.getId_bac_dao_tao());
		map.put("id_don_vi_ql", n.getId_don_vi_ql());
		return this.conn.Insert("NGANH", map);
	}

	/**
	 * @function update Nganh
	 * @param n
	 * @return
	 * @throws Exception
	 */
	public boolean Update(NganhM n) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ma", n.getMa());
		map.put("ten", n.getTen());
		map.put("id_bac_dao_tao", n.getId_bac_dao_tao());
		map.put("id_don_vi_ql", n.getId_don_vi_ql());
		return this.conn.Update("NGANH", map, "id_nganh = " + n.getId_nganh());
	}

	/**
	 * @function delete Nganh
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("NGANH", "id_nganh = " + id);
	}
}